package web;

import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class LoginServlet extends BaseServlet {
    private UserService userService=new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if(userService.existsUsername(username)) {
            System.out.println("用户["+username+"]存在");
            if (userService.login(new User(null, username, password, "")) == null) {
                System.out.println("密码错误");
                request.setAttribute("msg","密码错误！");
                request.setAttribute("username",username);
                request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
            } else {
                System.out.println("欢迎:" + username);
                request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request, response);
            }
        }
        else{
            request.setAttribute("msg","用户名不存在");
            System.out.println("用户名[" + username+"]不存在");
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }
    }
}
